gtk-demo: Make fishbowl info text use bindings
authorBenjamin Otte <otte@redhat.com>
Mon, 25 Nov 2019 06:28:19 +0000 (07:28 +0100)
committerMatthias Clasen <mclasen@redhat.com>
Sat, 30 May 2020 23:26:44 +0000 (19:26 -0400)
It's a good demo for how bindings can format multiple properties into an
informative string with 1 line of code (and 5 lines of XML).

demos/gtk-demo/fishbowl.c
demos/gtk-demo/fishbowl.ui
demos/gtk-demo/gtkfishbowl.c

index 6cc2dfc1a2ecc47f8c5628549764faeee140db9e..bc3848fb189352a96fe8702ae1ee7cc4db2785d4 100644 (file)
@@ -249,6 +249,14 @@ fishbowl_changes_toggled_cb (GtkToggleButton *button,
     gtk_button_set_icon_name (GTK_BUTTON (button), "changes-allow");
 }
 
+char *
+format_header_cb (GObject *object,
+                  guint    count,
+                  double   fps)
+{
+  return g_strdup_printf ("%u Icons, %.2f fps", count, fps);
+}
+
 GtkWidget *
 do_fishbowl (GtkWidget *do_widget)
 {
index fcf5d38fa4f473a2770f80cd8bfa8c13babe9409..b174e416fbcda755afda7cb252e38dcb48229fac 100644 (file)
         </child>
         <child type="end">
           <object class="GtkLabel">
-            <property name="label">fps</property>
-          </object>
-        </child>
-        <child type="end">
-          <object class="GtkLabel">
-            <property name="label" bind-source="bowl" bind-property="framerate-string"/>
-          </object>
-        </child>
-        <child type="end">
-          <object class="GtkLabel">
-            <property name="label">Icons, </property>
-          </object>
-        </child>
-        <child type="end">
-          <object class="GtkLabel">
-            <property name="label" bind-source="bowl" bind-property="count"/>
+            <binding name="label">
+              <closure type="gchararray" function="format_header_cb">
+                <lookup name="count">bowl</lookup>
+                <lookup name="framerate">bowl</lookup>
+              </closure>
+            </binding>
           </object>
         </child>
         <child type="end">
index 0d9f85f12426c183139963a0f98562dc606c643e..58c2c5a182ceb61c58a732dc3d60ba028018acc6 100644 (file)
@@ -53,7 +53,6 @@ enum {
    PROP_BENCHMARK,
    PROP_COUNT,
    PROP_FRAMERATE,
-   PROP_FRAMERATE_STRING,
    PROP_UPDATE_DELAY,
    NUM_PROPERTIES
 };
@@ -289,14 +288,6 @@ gtk_fishbowl_get_property (GObject         *object,
       g_value_set_double (value, gtk_fishbowl_get_framerate (fishbowl));
       break;
 
-    case PROP_FRAMERATE_STRING:
-      {
-        char *s = g_strdup_printf ("%.2f", gtk_fishbowl_get_framerate (fishbowl));
-        g_value_set_string (value, s);
-        g_free (s);
-      }
-      break;
-
     case PROP_UPDATE_DELAY:
       g_value_set_int64 (value, gtk_fishbowl_get_update_delay (fishbowl));
       break;
@@ -350,13 +341,6 @@ gtk_fishbowl_class_init (GtkFishbowlClass *klass)
                            0,
                            G_PARAM_READABLE);
 
-  props[PROP_FRAMERATE_STRING] =
-      g_param_spec_string ("framerate-string",
-                           "Framerate as string",
-                           "Framerate as string, with 2 decimals",
-                           NULL,
-                           G_PARAM_READABLE);
-
   props[PROP_UPDATE_DELAY] =
       g_param_spec_int64 ("update-delay",
                           "Update delay",
@@ -508,7 +492,6 @@ gtk_fishbowl_do_update (GtkFishbowl *fishbowl)
   priv->framerate = ((int)(priv->framerate * 100))/100.0;
 
   g_object_notify_by_pspec (G_OBJECT (fishbowl), props[PROP_FRAMERATE]);
-  g_object_notify_by_pspec (G_OBJECT (fishbowl), props[PROP_FRAMERATE_STRING]);
 
   if (!priv->benchmark)
     return;